package com.campus.client.view;

import com.campus.client.controller.LoginController;
import com.campus.client.controller.StudentController;
import com.campus.client.net.ClientConnector;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class StudentRegisterFrame {

    private TextField nameField=new TextField();
    private TextField ageField=new TextField();
    private TextField gradeField=new TextField();
    private TextField majorField=new TextField();
    private TextField contactField=new TextField();
    private TextField genderField=new TextField();
    private TextField classField=new TextField();
    private TextField enrollmentDateField=new TextField();
    private TextField recordIdField=new TextField();
    private Button registerBtn = new Button("注册");;
    private Button backBtn = new Button("返回");
    private String studentId;
    private String password;

    private StudentController controller;

    public String getStudentId() {
        return studentId;
    }
    public String getPassword() {
        return password;
    }

    public void show(Stage primaryStage, String studentId, String password) throws IOException {


        this.studentId=studentId;
        this.password=password;
        // 创建主VBox容器
        VBox mainVBox=new VBox(10);

        ClientConnector connector = new ClientConnector("172.20.10.2", 8888);
        controller = new StudentController(this, connector);

        mainVBox.setPrefHeight(400.0);
        mainVBox.setPrefWidth(640.0);

        // 创建AnchorPane
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxHeight(Double.NEGATIVE_INFINITY);
        anchorPane.setMaxWidth(Double.NEGATIVE_INFINITY);
        anchorPane.setPrefHeight(651.0);
        anchorPane.setPrefWidth(719.0);
        AnchorPane.setTopAnchor(anchorPane, 0.0);
        AnchorPane.setBottomAnchor(anchorPane, 0.0);
        AnchorPane.setLeftAnchor(anchorPane, 0.0);
        AnchorPane.setRightAnchor(anchorPane, 0.0);

        // 创建顶部蓝色矩形
        Rectangle topRect = new Rectangle();
        topRect.setArcHeight(5.0);
        topRect.setArcWidth(5.0);
        topRect.setFill(Color.DODGERBLUE);
        topRect.setHeight(83.0);
        topRect.setStroke(Color.BLACK);
        topRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        topRect.setWidth(640.0);

        // 创建标题文本
        Text titleText = new Text("学生注册账号");
        titleText.setFill(Color.WHITE);
        titleText.setLayoutX(211.0);
        titleText.setLayoutY(55.0);
        titleText.setStrokeWidth(0.0);
        titleText.setWrappingWidth(217.99998474121094);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 36.0));

        // 创建主内容HBox
        HBox mainHBox = new HBox();
        mainHBox.setLayoutX(30.0);
        mainHBox.setLayoutY(115.0);
        mainHBox.setPrefHeight(219.0);
        mainHBox.setPrefWidth(580.0);
        mainHBox.setSpacing(30.0);

        // 创建左侧VBox
        VBox leftVBox = new VBox();
        leftVBox.setPrefHeight(245.0);
        leftVBox.setPrefWidth(268.0);
        leftVBox.setSpacing(10.0);

        // 姓名HBox
        HBox nameHBox = new HBox();
        nameHBox.setPrefHeight(36.0);
        nameHBox.setPrefWidth(100.0);

        Text nameLabel = new Text("姓名：");
        nameLabel.setStrokeWidth(0.0);
        nameLabel.setWrappingWidth(75.99998474121094);
        nameLabel.setFont(Font.font(24.0));

        nameField.setPrefHeight(31.0);
        nameField.setPrefWidth(191.0);

        nameHBox.getChildren().addAll(nameLabel, nameField);

        // 年龄HBox
        HBox ageHBox = new HBox();
        ageHBox.setPrefHeight(34.0);
        ageHBox.setPrefWidth(100.0);

        Text ageLabel = new Text("年龄：");
        ageLabel.setStrokeWidth(0.0);
        ageLabel.setWrappingWidth(75.99998474121094);
        ageLabel.setFont(Font.font(24.0));

        ageField.setPrefHeight(31.0);
        ageField.setPrefWidth(191.0);

        ageHBox.getChildren().addAll(ageLabel, ageField);

        // 年级HBox
        HBox gradeHBox = new HBox();
        gradeHBox.setPrefHeight(33.0);
        gradeHBox.setPrefWidth(100.0);

        Text gradeLabel = new Text("年级：");
        gradeLabel.setStrokeWidth(0.0);
        gradeLabel.setWrappingWidth(75.99998474121094);
        gradeLabel.setFont(Font.font(24.0));

        gradeField.setPrefHeight(31.0);
        gradeField.setPrefWidth(191.0);

        gradeHBox.getChildren().addAll(gradeLabel, gradeField);

        // 专业HBox
        HBox majorHBox = new HBox();
        majorHBox.setPrefHeight(33.0);
        majorHBox.setPrefWidth(100.0);

        Text majorLabel = new Text("专业：");
        majorLabel.setStrokeWidth(0.0);
        majorLabel.setWrappingWidth(75.99998474121094);
        majorLabel.setFont(Font.font(24.0));

        majorField.setPrefHeight(31.0);
        majorField.setPrefWidth(191.0);

        majorHBox.getChildren().addAll(majorLabel, majorField);

        // 联系方式HBox
        HBox contactHBox = new HBox();
        contactHBox.setPrefHeight(38.0);
        contactHBox.setPrefWidth(135.0);

        Text contactLabel = new Text("联系方式：");
        contactLabel.setStrokeWidth(0.0);
        contactLabel.setWrappingWidth(120.66661071777344);
        contactLabel.setFont(Font.font(24.0));

        contactField.setPrefHeight(31.0);
        contactField.setPrefWidth(148.0);

        contactHBox.getChildren().addAll(contactLabel, contactField);

        // 将左侧所有组件添加到leftVBox
        leftVBox.getChildren().addAll(nameHBox, ageHBox, gradeHBox, majorHBox, contactHBox);

        // 创建右侧VBox
        VBox rightVBox = new VBox();
        rightVBox.setPrefHeight(263.0);
        rightVBox.setPrefWidth(280.0);
        rightVBox.setSpacing(10.0);

        // 性别HBox
        HBox genderHBox = new HBox();
        genderHBox.setAlignment(Pos.CENTER_LEFT);
        genderHBox.setPrefHeight(26.0);
        genderHBox.setPrefWidth(411.0);

        Text genderLabel = new Text("性别：");
        genderLabel.setStrokeWidth(0.0);
        genderLabel.setWrappingWidth(75.99998474121094);
        genderLabel.setFont(Font.font(24.0));

        genderField.setPrefHeight(31.0);
        genderField.setPrefWidth(215.0);

        genderHBox.getChildren().addAll(genderLabel, genderField);

        // 班级HBox
        HBox classHBox = new HBox();
        classHBox.setAlignment(Pos.CENTER_LEFT);
        classHBox.setPrefHeight(37.0);
        classHBox.setPrefWidth(280.0);

        Text classLabel = new Text("班级：");
        classLabel.setStrokeWidth(0.0);
        classLabel.setWrappingWidth(75.99998474121094);
        classLabel.setFont(Font.font(24.0));

        classField.setPrefHeight(31.0);
        classField.setPrefWidth(219.0);

        classHBox.getChildren().addAll(classLabel, classField);

        // 入学日期HBox
        HBox enrollmentDateHBox = new HBox();
        enrollmentDateHBox.setAlignment(Pos.CENTER_LEFT);
        enrollmentDateHBox.setPrefHeight(37.0);
        enrollmentDateHBox.setPrefWidth(274.0);

        Text enrollmentDateLabel = new Text("入学日期：");
        enrollmentDateLabel.setStrokeWidth(0.0);
        enrollmentDateLabel.setWrappingWidth(121.99998617172241);
        enrollmentDateLabel.setFont(Font.font(24.0));


        enrollmentDateField.setPrefHeight(31.0);
        enrollmentDateField.setPrefWidth(174.0);

        enrollmentDateHBox.getChildren().addAll(enrollmentDateLabel, enrollmentDateField);

        // 学籍号HBox
        HBox studentIdHBox = new HBox();
        studentIdHBox.setAlignment(Pos.CENTER_LEFT);
        studentIdHBox.setPrefHeight(35.0);
        studentIdHBox.setPrefWidth(359.0);

        Text studentIdLabel = new Text("学籍号：");
        studentIdLabel.setStrokeWidth(0.0);
        studentIdLabel.setWrappingWidth(97.99998408555984);
        studentIdLabel.setFont(Font.font(24.0));


        recordIdField.setPrefHeight(31.0);
        recordIdField.setPrefWidth(201.0);

        studentIdHBox.getChildren().addAll(studentIdLabel, recordIdField);

        // 将右侧所有组件添加到rightVBox
        rightVBox.getChildren().addAll(genderHBox, classHBox, enrollmentDateHBox, studentIdHBox);

        // 将左右VBox添加到主HBox
        mainHBox.getChildren().addAll(leftVBox, rightVBox);

        // 创建注册按钮

        registerBtn.setLayoutX(496.0);
        registerBtn.setLayoutY(334.0);
        registerBtn.setMnemonicParsing(false);
        registerBtn.setPrefHeight(30.0);
        registerBtn.setPrefWidth(107.0);
        registerBtn.setStyle("-fx-background-color: #1E90FF;");
        registerBtn.setTextFill(Color.WHITE);
        registerBtn.setFont(Font.font(24.0));

        // 创建返回按钮

        backBtn.setLayoutX(369.0); // 496 - 107 - 20 = 369 (注册按钮X坐标 - 按钮宽度 - 间距)
        backBtn.setLayoutY(334.0); // 与注册按钮相同的Y坐标
        backBtn.setMnemonicParsing(false);
        backBtn.setPrefHeight(30.0);
        backBtn.setPrefWidth(107.0);
        backBtn.setStyle("-fx-background-color: #1E90FF;");
        backBtn.setTextFill(Color.WHITE);
        backBtn.setFont(Font.font(24.0));

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(topRect, titleText, mainHBox, registerBtn, backBtn);

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);


        // 创建场景并显示
        Scene scene = new Scene(mainVBox);
        primaryStage.setTitle("学生注册");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public String getName(){return nameField.getText();}
    public String getGender(){return genderField.getText();}
    public String getAge(){return ageField.getText();}
    public String getClassName(){return classField.getText();}
    public String getEnrollmentDate(){return enrollmentDateField.getText();}
    public String getRecordId(){return recordIdField.getText();}
    public String getMajor(){return majorField.getText();}
    public String getGrade(){return gradeField.getText();}
    public String getContact(){return contactField.getText();}

    public void setRegisterButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        registerBtn.setOnAction(handler::accept);
    }

    public void setBackButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        backBtn.setOnAction(handler::accept);
    }

    public void showLoginFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) registerBtn.getScene().getWindow();

        // 创建主界面
        LoginFrame loginFrame = new LoginFrame();
        Stage loginStage = new Stage();

        // 显示主界面
        loginFrame.start(loginStage);

        // 关闭登录窗口
        registerStage.close();
    }

}
